# -*- coding: utf-8 -*-

class PrimeError(Exception):pass
class OutOfRangeError(PrimeError):pass
class NotIntegerError(PrimeError):pass
class InvalidOrderError(PrimeError):pass

def prime_short(lhs,rhs):
    ''' 所求的素数范围较小时'''

    stop = int(rhs**0.5)
    candidates = range(rhs+1)
    for i in xrange(2,stop+1):
        if not candidates[i]:
            continue
        candidates[2*i::i] = [None]*(rhs//i-1)
        
    return [i for i in candidates[2:] if i>=lhs]

def prime_long(lhs,rhs):
    """ 所求的素数范围较大时"""
    pass

    
def prime(lhs,rhs):
    '''判断 lhs 到 rhs 区间内的每个整数是否为素数，返回存有该区间内所有素数的列表'''
    
    if not ( isinstance(lhs,int) or isinstance(lhs,long) ) or\
       not ( isinstance(rhs,int) or isinstance(rhs,long) ):
        raise NotIntegerError, 'Arguments must be integer'
      
    if lhs > rhs:
        raise InvalidOrderError, 'Arguments error, left argument must little than right argument' 
    
    if lhs <=0 or rhs <=0:
        raise OutOfRangeError, 'Arugments error, arguments must be postive integer'
           
    resList = []
    
    if lhs == 1 and lhs == rhs:
        return resList
    elif lhs == 1:
        lhs = 2
    
    import sys
    
    # 分情况讨论，当所给出的数较大时，需特殊处理，否则容易内存溢出 
    if  rhs <= sys.maxint/100 :
        resList = prime_short(lhs,rhs) 
    else:
        resList = prime_long(lhs,rhs)
                      
    return resList


if __name__ == "__main__":
    lst = prime(2,1000)
    print len(lst),lst
    
    
    
    
